package com.xk.ucs.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * @ClassName Result
 * @Description 接口返回结果对象
 * @Author 01387843
 * @Date 2022/6/22
 **/
@Data
@ApiModel("接口返回包装类")
public class Result<T> implements Serializable {
    private String code;
    @ApiModelProperty("返回的结果")
    private T data;
    @ApiModelProperty("错误信息，给开发者使用。（可选）")
    private String message;
    @ApiModelProperty("提示信息，终端用户使用。（可选）")
    private String info;
    @ApiModelProperty("请求id")
    private String requestId;
    private final static String successCode = "200";
    private final static String failCode = "-1";
    private final static String failMsg = "failure";
    private final static String successMsg = "success";

    public Result(T data) {
        this.data = data;
        this.code = successCode;
        this.message = successMsg;
    }

    public Result() {
        this.code = successCode;
        this.message = successMsg;
    }

    public static <T> Result<T> DefaultFailure(String msg) {
        Result result = new Result();
        result.setCode(failCode);
        result.setMessage(msg);
        return result;
    }
    public static <T> Result<T> failure(ErrorEnum errorEnum) {
        Result result = new Result();
        result.setCode(errorEnum.getCode());
        result.setMessage(errorEnum.getMessage());
        return result;
    }
    public static <T> Result<T> failure(String message) {
        Result result = new Result();
        result.setCode(failCode);
        result.setMessage(message);
        return result;
    }
    public static <T> Result<T> DefaultFailure(String code ,String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMessage(msg);
        return result;
    }
    public boolean isSuccess() {
        return successCode.equals(this.code);
    }

    public static <T> Result<T> success() {
        return new Result();
    }

    public static  <T> Result<T>   success(Object data) {
        return new Result(data);
    }
    public Result<T> setCode(final String code) {
        this.code = code;
        return this;
    }

    public Result<T> setData(final T data) {
        this.data = data;
        return this;
    }
    public Result<T> setMessage(final String message) {
        this.message = message;
        return this;
    }

    public Result<T> setInfo(final String info) {
        this.info = info;
        return this;
    }
    @Override
    public String toString() {
        return "Result(code=" + this.getCode() + ", data=" + this.getData() + ", message=" + this.getMessage() + ", info=" + this.getInfo() + ")";
    }
}
